﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using SmartCity.ProcessEngine.API.ErrorCode;
using SmartCity.ProcessEngine.API.Write.Applications.Commands;

namespace SmartCity.ProcessEngine.API.Write.Controllers
{

    /// <summary>
    /// API控制器—写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ManagementController : ControllerBase
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<ManagementController> logger;
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="mediator">中介者</param>
        public ManagementController(ILogger<ManagementController> logger, IMediator mediator)
        {
            this.logger = logger;
            this.mediator = mediator;
        }
        /// <summary>
        /// 表单分类添加方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> AddFormType(FromTypeCreatedCommand command)
        {
            try
            {
                logger.LogInformation("表单分类添加成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("表单分类添加失败进入异常" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 表单分类修改方法
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPost]
        public async Task<APIResult<int>> UpdateType(FormTypeUpdateCommand command)
        {
            try
            {
                logger.LogInformation("表单分类修改成功");
                return await mediator.Send(command);
            }
            catch (Exception ex)
            {
                logger.LogError("表单分类修改失败进入异常" + ex.Message);
                throw;
            }
        }
    }
}
